/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package wap.rockvilletech.xms;

import org.apache.log4j.Logger;

/**
 * 
 * @author Aashiqbhatti
 */
public class SendXMSManager {

    private static final Logger log = Logger.getLogger(SendXMSManager.class);
	private static final String newLine = "\\\\n";

	public static String sendWAPPush(String strGSMNumber, String providerName, String shortcode, String wapText, String wapURL) {
		boolean isSave = false;
		try {
			if (!strGSMNumber.startsWith("+")) {
				strGSMNumber = "+" + strGSMNumber;
			}
			SiWapPush wp = new SiWapPush(wapURL, wapText);			
			StringBuffer sendxms = new StringBuffer("[");
			sendxms.append(providerName);
			sendxms.append("]\nCount=0\nOriginatingAddress=");
			sendxms.append(shortcode);
			sendxms.append("\nPhone=");
			sendxms.append(strGSMNumber);
			sendxms.append("\nLocalId=root\nDCS=245\nUDH=05040B8423F0\r\nXMS=");
			sendxms.append(wp.toSmsBinary());
			sendxms.append("\n");
			String sendxmsWAP = sendxms.toString();
			
			isSave = DBProcSendXMS.getInstance().saveXMS(sendxmsWAP, providerName);
			log.info("sendWAPPush("+strGSMNumber+", "+shortcode+", "+providerName+") processed, status="+isSave);
			
		} catch (Exception e) {
			log.error("processWAPPush("+strGSMNumber+", "+providerName+", "+shortcode+", "+wapText+", "+wapURL+") " + e);
		}
		return isSave?"1":"0";
	} // end sendSMSText

	/**
	 * 
	 * @param strGSMNumber
	 * @param smsText
	 * @param shortcode
	 * @param providerName
	 */
	public static String sendXMS(String strGSMNumber, String smsText, String shortcode, String providerName) {
		boolean isSave = false;
		if (!strGSMNumber.startsWith("+")) {
			strGSMNumber = "+" + strGSMNumber;
		}
		try {
			smsText = smsText.replaceAll("~", newLine);
			byte [] smsBytes = smsText.getBytes("UTF-8");			
			smsText = new String(smsBytes, "UTF-8");
			
			StringBuffer sendxms = new StringBuffer("[");
			sendxms.append(providerName);
			sendxms.append("]\nCount=0\nOriginatingAddress=");
			sendxms.append(shortcode);
			sendxms.append("\nPhone=");
			sendxms.append(strGSMNumber);
			sendxms.append("\nSplit=25\nLocalId=root\r\nXMS=");
			sendxms.append(smsText);
			sendxms.append("\n");
			String xms = sendxms.toString();
			// log.debug("sendxms -  = "+sendxms);

			isSave = DBProcSendXMS.getInstance().saveXMS(xms, providerName);
			log.info("sendXMS("+strGSMNumber+", "+shortcode+", "+providerName+") processed, status="+isSave);
		} catch (Exception e) {
			log.error("sendXMS("+strGSMNumber+", "+smsText+", "+shortcode+", "+providerName+") " + e);
		}
		return isSave?"1":"0";
	}

}// END CALSSS
